package dao.impl;

import dao.DlbDao;
import entity.DlbEntity;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Lei
 * @create 2021-04-15 18:54
 */
@Repository
public class DlbDaoImpl implements DlbDao {

    @Resource
    private HibernateTemplate hibernateTemplate;

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    @Override
    public DlbEntity findByXhAndKl(String xh, String kl) {
        DlbEntity dlbEntity = new DlbEntity();
        dlbEntity.setKl(kl);
        dlbEntity.setXh(xh);
        List<DlbEntity> byExample = (List<DlbEntity>) this.hibernateTemplate.find("from DlbEntity where xh = ? and kl =?",xh,kl);
        if (byExample.size()!=0){
            return byExample.get(byExample.size()-1);
        }else{
            return null;
        }
    }

    @Override
    @Transactional
    public int addDlbRecord(DlbEntity dlbEntity) {
        this.hibernateTemplate.saveOrUpdate(dlbEntity);
        return 1;
    }

    @Override
    @Transactional
    public int updateRecord(DlbEntity dlbEntity) {
        this.hibernateTemplate.update(dlbEntity);
        return 1;
    }
}
